package com.honey.mybatis.generator.condition.method;

import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.condition.ConditionConstant;
import com.honey.mybatis.generator.criteria.CriteriaConstant;

/**
 * clear 函数
 * @author Administrator
 *
 */
public class ConstructorMethod extends AbstractMethod {
	
	public ConstructorMethod(){
	}
	public ConstructorMethod(Clazz clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	private static final String METHOD_NAME = ConditionConstant.CONDITION_CLASS_NAME;
	
	/** 参数 */
	private static final Parameter[] PARAMETER= new Parameter[]{
		new Parameter(new FullyQualifiedJavaType("T"),ConditionConstant.GENERATED_CRITERIA)
	};
	
	private static final String[] JAVA_DOC = new String []{
		"构造方法。"
	};
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {
		return METHOD_NAME;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		return JAVA_DOC;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getParameter()
	 */
	@Override
	protected Parameter[] getParameter() {
		
		return PARAMETER;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		method.addBodyLine((new StringBuilder()).append(ConditionConstant.ORED_CRITERIA).append(" = new ArrayList<T>()").toString());
		method.addBodyLine((new StringBuilder()).append(JavaKeyWord.OPERATION_THIS).append(ConditionConstant.GENERATED_CRITERIA).append(" = ").append(ConditionConstant.GENERATED_CRITERIA).toString());
		IfBlock block = new IfBlock();
		block.setCondition((new StringBuilder()).append(ConditionConstant.GENERATED_CRITERIA).append(".").append(CriteriaConstant.GET_CRITERIA).append("().size() > 0").toString());
		block.addBodyLine((new StringBuilder()).append(ConditionConstant.ORED_CRITERIA).append(".add(").append(ConditionConstant.GENERATED_CRITERIA).append(")").toString());
		method.addBlocks(block);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodDecoration()
	 */
	@Override
	protected MethodDecoration getMethodDecoration() {
		MethodDecoration methodDecoration =  super.getMethodDecoration();
		methodDecoration.setConstructor(true);
		return methodDecoration;
	}
	
	public static void main(String []args){
		ConstructorMethod  cm = new ConstructorMethod();
		System.out.println(cm.createMethod().compiledContent(0)); 
	}
}
